package com.mobilepay.moneypig.model.activity.launcher;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.NumberKeyListener;
import android.view.View;
import android.widget.EditText;

import com.githang.statusbar.StatusBarCompat;
import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.base.NoDoubleClickListener;
import com.mobilepay.moneypig.config.Key;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.response.pro.UserInfo;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;
import com.mobilepay.moneypig.utils.PreferenceUtil;

/**
 * Created by xlm on 2017/7/13 0013
 */

public class LoginActivity extends BaseActivity {

    private final String TAG = "LoginActivity";
    private EditText mMobile, mPs;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void afterSetContentView() {
        super.afterSetContentView();
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#40000000"));
    }

    @Override
    protected void initViews() {
        mMobile = (EditText) findViewById(R.id.login_mobile);
        //只输入数字
        mMobile.setKeyListener(new NumberKeyListener() {
            @Override
            public int getInputType() {
                return android.text.InputType.TYPE_CLASS_PHONE;
            }

            @Override
            protected char[] getAcceptedChars() {
                return new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
            }
        });
        mPs = (EditText) findViewById(R.id.login_pw);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        mobile = (String) PreferenceUtil.getParam(getThis(), Key.USERNAME, "");
        password = (String) PreferenceUtil.getParam(getThis(), Key.PASSWORD, "");
        if (!CommonUtil.isEmpty(mobile)) {
            mMobile.setText(mobile);
            mMobile.setSelection(mobile.length());
        } else {
            mMobile.setText(null);
        }
        if (!CommonUtil.isEmpty(password)) {
            mPs.setText(password);
        } else {
            mPs.setText(null);
        }
    }

    @Override
    protected void onSetListener() {
        findViewById(R.id.login_regist).setOnClickListener(this);
        findViewById(R.id.login_forget).setOnClickListener(this);
        findViewById(R.id.login_denglu).setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                super.onNoDoubleClick(view);
                mobile = mMobile.getText().toString();
                password = mPs.getText().toString();
                onLogin();
            }
        });
    }

    private static final int RESUST_REGIST = 1;
    private static final int RESUST_FORGET = 2;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.login_regist:
                Intent intent1 = new Intent();
                intent1.setClass(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent1, RESUST_REGIST);
                break;
            case R.id.login_forget:
                Intent intent2 = new Intent();
                intent2.setClass(LoginActivity.this, ForgetActivity.class);
                startActivityForResult(intent2, RESUST_FORGET);
                break;
        }
    }

    String mobile;
    String password;

    /**
     * 登录
     */
    private void onLogin() {
        if (CommonUtil.isEmpty(mobile)) {
            showToastShort("请输入手机号码");
            return;
        }
        if (!CommonUtil.isMobile(mobile)) {
            showToastShort("请输入正确的手机号码");
            return;
        }
        if (CommonUtil.isEmpty(password)) {
            showToastShort("请输入密码");
            return;
        }
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.login);
        param.add("username", mobile);
        param.add("password", password);
        param.add("user_type", "2");
        param.add("sign", param.getMd5String());
        showProgress("登陆中");
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissProgress();
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissProgress();
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (responseResult.getStatus() == 1) {
                        UserInfo userInfo = GsonUtil.GsonToBean(string, UserInfo.class);
                        App.getApp().setUserInfo(userInfo);//保存用户信息
                        PreferenceUtil.setParam(getThis(), Key.USERNAME, mobile);
                        PreferenceUtil.setParam(getThis(), Key.PASSWORD, password);
                        startActivity(MainActivity.class, true, true);
                    } else {
                        showToastShort(responseResult.getInfo());
                    }
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case RESUST_REGIST:
                    mobile = data.getStringExtra(Key.USERNAME);
                    password = data.getStringExtra(Key.PASSWORD);
                    if (!CommonUtil.isEmpty(mobile)) {
                        mMobile.setText(mobile);
                        mPs.setText(password);
                        mMobile.setSelection(mobile.length());
                        onLogin();//自动登录
                    }
                    break;
                case RESUST_FORGET:
                    mobile = data.getStringExtra(Key.USERNAME);
                    password = data.getStringExtra(Key.PASSWORD);
                    if (!CommonUtil.isEmpty(password)) {
                        mMobile.setText(mobile);
                        mPs.setText(password);
                        mPs.setSelection(mPs.length());
                    }
                    break;
            }
        }
    }
}
